{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linked Brushing Demo\n",
    "\n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/anitagraser/movingpandas-examples/main?filepath=3-tech-demos/linked-brushing.ipynb)\n",
    "\n",
    "This notebook demonstrates linked brushing with **Holoviews.selection**.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "import holoviews as hv\n",
    "import hvplot.pandas  # noqa\n",
    "from datashader.utils import lnglat_to_meters\n",
    "from holoviews.element import tiles\n",
    "from holoviews.util.transform import dim\n",
    "from holoviews.selection import link_selections\n",
    "from holoviews.operation import gridmatrix\n",
    "from holoviews.operation.element import histogram\n",
    "from holoviews import opts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "opts.defaults(opts.Overlay(active_tools=['wheel_zoom']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gpd.read_file('../data/ais.gpkg', rows=1000)\n",
    "gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.loc[:, 'x'], gdf.loc[:, 'y'] = lnglat_to_meters(gdf.geometry.x, gdf.geometry.y)\n",
    "df = pd.DataFrame(gdf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas DataFrame.hvplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hist_plot = df.where((df.SOG>0) & (df.SOG<50)).hvplot.hist(\"SOG\",  bins=20, width=400, height=300) \n",
    "map_plot = df.hvplot.scatter(x='x', y='y', width=400, height=300)\n",
    "link_selections(tiles.CartoLight() * map_plot + hist_plot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geopandas GeoDataFrame.hvplot\n",
    "\n",
    "To use the GeoDataFrame directly, we need to explicitly set a suitable index for linking, as described in https://github.com/holoviz/geoviews/issues/497"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf['id'] = np.arange(len(gdf))\n",
    "gdf_map = gdf.hvplot(geo=True, tiles='CartoLight', width=400, height=300)\n",
    "gdf_hist = pd.DataFrame(gdf).where((gdf.SOG>0) & (gdf.SOG<50)).hvplot.hist(\"SOG\",  bins=20, width=400, height=300)\n",
    "link_selections(gdf_map + gdf_hist, index_cols=['id'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Datashade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datashade = df.hvplot.scatter(x='x', y='y', datashade=True, width=400, height=300)\n",
    "link_selections(datashade + hist_plot)#.cols(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "link_selections(tiles.CartoLight() * datashade + hist_plot)#.cols(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bar plots (unsupported)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It would be nice to add a bar plot with counts per ship type but bar plots are currently not supported, see http://holoviews.org/user_guide/Linked_Brushing.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar_plot = df.groupby('ShipType').agg({'SOG':'count'}).rename(columns={'SOG':'count'}).hvplot.barh(width=400, height=400)\n",
    "bar_plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
